package com.metronoteapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.metronoteapp.GroupExpActivity;
import com.metronoteapp.R;
import com.metronoteapp.database.GroupNoteDao;
import com.metronoteapp.database.NoteDao;
import com.metronoteapp.model.GroupModel;
import com.metronoteapp.model.NoteModel;
import com.metronoteapp.until.Constants;

public class DialogClient extends Dialog implements
		android.view.View.OnClickListener {
	private String TAG = DialogClient.class.getSimpleName();
	private Context mContext;
	private Dialog dialog;
	private TextView txtMessage;
	private Button btnOk;
	private Button btnCancel;
	private String mType;
	private String mIdGroup;

	public DialogClient(Context context) {
		super(context);
		mContext = context;
	}

	public void displaydialog(String _message, String _type, String _idGroup) {
		dialog = new Dialog(mContext);
		mType = _type;
		mIdGroup = _idGroup;
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.layout_dialog_comfirm);
		// dialog.getWindow().getAttributes().windowAnimations =
		// R.style.Animations_SmileWindow;
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		txtMessage = (TextView) dialog.findViewById(R.id.txtMessage);
		btnOk = (Button) dialog.findViewById(R.id.btnOk);
		btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
		txtMessage.setText(_message);
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		dialog.show();
	}

	/*
	 * This function parse cursor
	 */
	private static String getColumnAsString(Cursor cursor, int index) {
		return cursor.getString(index).trim();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnOk:
			if (mType.equalsIgnoreCase(Constants.TYPE_EXIT_APP)) {
				dialog.dismiss();
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(intent);
			} else if (mType.equalsIgnoreCase(Constants.TYPE_DEL_GROUP)) {
				Boolean blnCheckOk = false;
				GroupNoteDao mObjGroupDao = new GroupNoteDao(mContext);
				GroupModel tempObj = new GroupModel();
				tempObj.setGroupId(Integer.parseInt(mIdGroup));

				// Delete group
				mObjGroupDao.openDB();
				blnCheckOk = mObjGroupDao.removeGroup(tempObj);
				Log.v(TAG, "DELETED : " + blnCheckOk);
				mObjGroupDao.closeDB();

				// Update note
				if (blnCheckOk == true) {
					NoteDao mObjNoteDao = new NoteDao(mContext);
					mObjNoteDao.openDB();
					NoteModel mObjNoteModel = new NoteModel();
					mObjNoteModel.setGroupId(tempObj.getGroupId());
					Cursor mCursor = mObjNoteDao.getNoteByGroup(mObjNoteModel);
					if (mCursor.moveToFirst()) {
						do {
							NoteModel mObjNoteModelGeted = new NoteModel();
							mObjNoteModelGeted.setId(Integer
									.parseInt(getColumnAsString(mCursor, 0)));
							mObjNoteModelGeted.setTitle(getColumnAsString(
									mCursor, 1));
							mObjNoteModelGeted.setContent(getColumnAsString(
									mCursor, 2));
							mObjNoteModelGeted.setNoteType(getColumnAsString(
									mCursor, 3));
							mObjNoteModelGeted.setDateTime(getColumnAsString(
									mCursor, 4));
							mObjNoteModelGeted.setGroupId(0);
							mObjNoteModelGeted.setFlgFav(Integer
									.parseInt(getColumnAsString(mCursor, 6)));

							mObjNoteDao.updateNote(mObjNoteModelGeted);
							Log.v(TAG,
									"UPDATE GROUP : "
											+ mObjNoteModelGeted.getTitle());
						} while (mCursor.moveToNext());
					}
					mObjNoteDao.closeDB();
				} else {
					Toast.makeText(mContext, "Error!", Toast.LENGTH_SHORT)
							.show();
				}
				Intent callExpandNote = new Intent(mContext,
						GroupExpActivity.class);
				mContext.startActivity(callExpandNote);
			}

			break;
		case R.id.btnCancel:
			dialog.dismiss();
			break;
		default:
			break;
		}
	}
}
